package cn.com.cetccst.illegalconnection.demo;

import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import cn.com.cetccst.illegalconnection.Address;
import cn.com.cetccst.illegalconnection.IllegalConnectionEngine;

public class MainActivity extends AppCompatActivity {
    private TextView logView;
    private final IllegalConnectionEngine illegalConnectionEngine = IllegalConnectionEngine.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        logView = findViewById(R.id.logView);
        findViewById(R.id.configBtn).setOnClickListener(this::config);
        findViewById(R.id.clearLogBtn).setOnClickListener(this::clearLog);
        illegalConnectionEngine.setLogger(this::log);
    }

    private void config(View view) {
        ConfigDlg.show(this, new ConfigDlg.Callback() {
            @Override
            public void handleAddressList(List<Address> addressList) {
                illegalConnectionEngine.setParams(addressList);
            }
        });
    }

    private void clearLog(View view) {
        logView.setText("");
    }

    private void log(String msg) {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            logView.setText(logView.getText().toString() + msg + "\n");
        } else {
            runOnUiThread(() -> log(msg));
        }
    }
}